package org.wxd.springcloud.swagger.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class SwaggerConfig {


//    @Bean
//    public Docket businessDocket() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .groupName("业务")
//                .select()//                   当前包路径
//                .apis(RequestHandlerSelectors.basePackage("org.wxd.springcloud.swagger.ctl.user"))
//                .paths(PathSelectors.any())
//                .build()
//                .globalOperationParameters(Lists.newArrayList(
//                        new ParameterBuilder()
//                                .name("token")
//                                .description("身份验证token")
//                                .modelRef(new ModelRef("String"))
//                                .parameterType("header")
//                                .required(true)
//                                .build()
//                ))
//                ;
//    }


    @Bean
    public Docket businessDocket() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("x-access-token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
//                .groupName("业务")
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.wxd.springcloud.swagger.ctl"))
                .paths(PathSelectors.regex(".*header.*"))
                .build()
                .globalOperationParameters(pars)
                .useDefaultResponseMessages(false)
                ;
    }


//    @Bean
//    public Docket certDocket() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .groupName("身份验证")
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("org.wxd.springcloud.swagger.ctl"))
//                .paths(PathSelectors.regex(".*noHeader.*"))
//                .build()
//                .useDefaultResponseMessages(false)
//                ;
//    }

    //构建api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("API")
                //创建人
                .contact(new Contact("","",""))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }

}
